// pages/management/particulars/index.js
const netWorked = require('../../../utils/nework.js')
const config = require('../../../utils/confing.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inpont: '',
    showView: true,
    list: [], //供应商
    list2: [], //仓库
    value: '',
    value2: "",
    indexTo: 0,
    goodsImg: [], //商品
    goodsAll: [],
    goodsId: [],
    numsCounts: 0,
    unit: 0,//总价格
    indexto: 0,
    supcust_no: 0,//供应商编码
    branch_no: 0,//商库编码
    detail: [],
    // valueX:''
  },
  // 显示遮罩层
  change: function () {
    let that = this;
    that.setData({
      showView: (!that.data.showView),
      indexTo: 0,
    })
  },
  // 隐藏遮罩层，获取选择供应商的值
  select: function (e) {
    let value = e.currentTarget.dataset.value
    let supcust_no = e.currentTarget.dataset.id
    // console.log(e)
    let that = this
    this.setData({
      value,
      showView: (!that.data.showView),
      supcust_no
    })
    wx.setStorageSync('value', value)
    // console.log()
    wx.setStorageSync('id', e.currentTarget.dataset.id)
    //供应商编码
  },
  // 隐藏遮罩层，选择的仓库
  select2: function (e) {
    // console.log(e)
    let branch_no = e.currentTarget.dataset.branch
    let value2 = e.currentTarget.dataset.value2
    let that = this
    this.setData({
      value2,
      showView: (!that.data.showView),
      branch_no
    })
    wx.setStorageSync('value2', value2)
  },
  // 隐藏遮罩层，并获取仓库
  cangku: function (e) {
    let that = this;
    that.setData({
      showView: (!that.data.showView),
      indexTo: 1,
    })
  },
  // 添加商品，跳转页面
  add: function (e) {
    let goodsAll = wx.getStorageSync('savesTides')
    let id = wx.getStorageSync("id")
    // console.log(id)
    if (id == "") {
      wx.showToast({
        title: '清先选择供应商',
        icon: "none",
      })
    } else if (goodsAll.length > 0) {
      wx.showModal({
        title: '请先保存订单，重新选择商品时此订单数据会清空',
        cancelText: '重新选择',
        confirmText: '保存订单',
        success: function (res) {
          if (res.confirm) {
            // that.save()
            console.log('用户点击确定')
          } else {

            wx.navigateTo({
              url: '/pages/management/administration/index',
            })

          }

        }
      })
    } else {

      wx.navigateTo({
        url: '/pages/management/administration/index',
      })
    }




  },
  // 获取供应商
  supplier: function () {
    let that = this
    netWorked.netWork({
      url: 'Archives/SelGongYin?where=' + '1=1',
      method: "POST",
      success: function (res) {
        // console.log(res)
        that.setData({
          list: res.data
        })
        // console.log(res)
      }
    })
  },
  // 获取仓库
  entrepot: function (e) {
    let that = this
    netWorked.netWork({
      url: '/GuanLiYuan/CangKu?where=' + '1=1',
      method: "POST",
      success: function (res) {
        that.setData({
          list2: res.data
        })
      }
    })
  },
  // 保存订单
  save: function (e) {
    let sheet_no = this.data.inpont//单号
    let branch_no = this.data.branch_no.toString()//仓库编码
    let supcust_no = this.data.supcust_no.toString()//供应商编码
    let master = []
    let that = this
    master.push({
      sheet_no: sheet_no,
      branch_no: branch_no,
      supcust_no: supcust_no
    })
    // console.log(this.data.detail)
    let detail = JSON.stringify(this.data.detail)
    let masterd = JSON.stringify(master)
    let where = 'PA'
    console.log(detail.replace(/\[\{/g, '{').replace(/\}\]/g, '}'))
    console.log(masterd)
    wx.request({
      url: config.BaseUrl + 'WxApplets/saveOrder?type=' + where,
      method: "POST",
      data: {
        'MASTER': masterd,
        'DETAIL': detail.replace(/\[\{/g, '{').replace(/\}\]/g, '}')
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        let data = res.data
        if (data==1){
          wx.showToast({
            title: '保存成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      }
    })
  },
  // 生成单号
  numbered: function () {
    let ly = ''
    let that = this
    netWorked.netWork({
      url: 'WxApplets/NewNumbers?tableType=' + 'pm' + '&type=' + 'PAD',
      method: "POST",
      success(res) {
        console.log(res.data)
        that.setData({
          inpont: res.data
        })
      }
    })
  },
  // 搜索
  onSearch(e) {
    console.log('123');
    let where = "supcust_no like '" + e.detail + "%25' or sup_name like '%25" + e.detail + "%25'";
    let _this = this
    let indexTo = this.data.indexTo
    netWorked.netWork({
      url: 'Archives/SelGongYin?where=' + where,
      method: "POST",
      success: function (res) {
        console.log(res.data)
        if (indexTo==0){
          _this.setData({
            list: res.data
          })
        } else if (indexTo==1){
          _this.setData({
            list2: res.data
          })
        }
        // let data = res.data
        // _this.setData({
        //   data: res.data
        // })
      }
    })
  },
  //审核
  saveShif:function(){
    let id = wx.getStorageSync("loginid")//登录的用户id
    let unit = this.data.unit
    let inpont = this.data.inpont
    netWorked.netWork({
      url: '/Purchase/ShengHe?where=' + inpont + "&JinE=" + unit +"&order_id=" +id,
      method: "POST",
      success(res) {
        let data = res.data
        if (data==0){
          wx.showToast({
            title: '提交核审成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        } 
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.supplier()
    this.entrepot()
    let that = this
    let indexto = parseInt(options.indexto)//页面传参判断是申请单还是订单
    this.setData({
      indexto
    })
    if (indexto == 0) {
      wx.setNavigationBarTitle({
        title: '采购申请单'
      })
    } else if (indexto == 1) {
      wx.setNavigationBarTitle({
        title: '采购订单'
      })
    }
    let inpont = this.data.inpont
    if (inpont == '') {
      that.numbered()
    }
    else {
      return false
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let value2 = wx.getStorageSync('value2')
    let value = wx.getStorageSync('value')
    let goodsAll = wx.getStorageSync('savesTides')
    let that = this
    this.setData({
      goodsAll: wx.getStorageSync('savesTides'),
      value,
      value2
    })
    let detail = []
    for (let i = 0; i < goodsAll.length; i++) {
      detail.push(goodsAll[i].goodsImgs)

    }
    let goods = wx.getStorageSync('savesTides')
    let imgade = []
    let numsCounts = 0
    let unit = 0
    let goodsId = this.data.goodsId
    for (let i = 0; i < goods.length; i++) {
      imgade = goods[i].goodsImgs
      numsCounts += goods[i].counts
      unit += goods[i].unit * goods[i].counts
      goodsId.push({
        images: goods[i].goodsImgs
      })
    }
    this.setData({
      goodsId,
      numsCounts,
      unit,
      detail
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})